<template>
  <div id="page" class="site">
    <div class="container">
      <div class="back">
        <div class="shape" v-html="RandomShape(512)"></div>
        <div class="shape" id="random" v-html="RandomShape(256)"></div>
      </div>
      <div class="hero">
        <h1>Sign In to<br>Open the World</h1>
        <p>If you don't have an account，<br>you can <a href="/sellerRegister">Register here</a>.</p>
      </div>
      <div class="main">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item><el-input type="text" v-model="loginForm.username" name="username" placeholder="Username"/></el-form-item>
          <el-form-item><el-input type="password" v-model="loginForm.password" name="password" placeholder="Password" @keyup.enter.native="handleLogin" show-password/></el-form-item>
          <el-button  :loading="loading" size="medium" type="primary" class="submit" @click.native.prevent="handleLogin">Login</el-button>
        </el-form>
        <div class="options">
          <div class="separator"><p>使用其他方式登录</p></div>
          <ul>
            <li><a href="#"><i class="ri-steam-fill ri-2x"></i></a></li>
            <li><a href="#"><i class="ri-playstation-fill ri-2x"></i></a></li>
            <li><a href="#"><i class="ri-xbox-fill ri-2x"></i></a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as blobs2 from "blobs/v2";
import colorful from "@/utils/colorful";
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { decrypt, encrypt } from "@/utils/jsencrypt";

export default{
  name: "sellerLogin",
  mounted() {
    this.RandomPosition()
  },
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "mojang",
        password: "mojang",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods:{
    RandomShape(size){
      return blobs2.svg(
          {seed: Math.random(), extraPoints: 10, randomness: 8, size: size},
          {fill: colorful.randomColor(), stroke: "", strokeWidth: 4});
    },
    RandomPosition(){
      let random = document.getElementById("random");
      random.style.top = 10 + Math.floor(Math.random() * 200) + "px";
      random.style.left = 10 + Math.floor(Math.random() * 200) + "px";
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("SellerLogin", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/backstage" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
}
</script>

<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@500&display=swap');
/*用中文替换原先英文字体*/
:root {
  --primary-color: #4460f1;
  --white-color: #ffffff;

  --light-text-color: #9398b3;
  --light-bg-color: #f2f4ff;
  --dark-color: #333333;

  --background-color: #fcfcff;
}

* {
  margin: 0;
  padding: 0;
}

*, ::before::after {
  box-sizing: border-box;
}

body {
  font-family: 'Noto Sans SC', sans-serif;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.8;
  color: var(--dark-color);
  background-color: var(--background-color);
  user-select: none;
  -webkit-user-drag: none;
}

a {
  text-decoration: none;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

ul {
  list-style: none;
}

h1 {
  font-size: 3em;
  font-weight: 700;
  line-height: normal;
  color: black;
}

.site {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.container {
  max-width: 1000px;
  width: 100%;
  padding: 0 30px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
}

.hero {
  flex: 1 0 66.6666%;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 50px;
  padding: 70px 0;
  text-align: left;
}

.shape {
  position: absolute;
  z-index: -2;
}

.hero a {
  font-weight: 500;
  color: var(--primary-color);
  transition: color .3s;
}

.hero a:hover,
form .password a:hover {
  color: var(--dark-color);
  text-decoration: underline;
}

.back{
  position: absolute;
  top: 10%;
  left: 10%;
  z-index: -1;
  filter: blur(50px);
}

.main {
  flex: 1 0 33.3333%;
}

.main form {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.main form p {
  position: relative;
}
.el-form-item{
  margin-bottom: 0;
}

input::placeholder {
  color: var(--dark-color);
  font-family: 'Noto Sans SC', sans-serif;
  font-size: inherit;
}

form .submit {
  font-weight: 700;
  box-shadow: #66b1ff 0 20px 30px -10px;
  cursor: pointer;
  transition: box-shadow .3s;
}

form .submit:hover {
  box-shadow: #66b1ff 0 10px 30px -10px;
}

.separator {
  position: relative;
  margin: 50px 0 30px;
}

.separator::before {
  content: '';
  position: absolute;
  top: 50%;
  width: 100%;
  height: 1px;
  background-color: var(--light-text-color);
  opacity: .3;
  z-index: 1;
}

.separator p {
  font-size: 14px;
  width: fit-content;
  padding: 0 10px;
  color: var(--light-text-color);
  background-color: var(--background-color);
  margin: 0 auto;
  position: relative;
  z-index: 1;
}

.options ul {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.options ul li a {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  border-radius: 10px;
  background-color: var(--white-color);
  box-shadow: var(--light-text-color) 0 10px 20px -10px;
  transition: all .3s ease-out;
}

.options ul li a:hover {
  color: var(--white-color);
  box-shadow: none;
}

/*用STEAM PSN XBOX替换原先APPLE GOOGLE FACEBOOK图标*/
.options ul li:nth-child(1) a:hover {
  background-color: #171a21;
}

.options ul li:nth-child(2) a:hover {
  background-color: #00439c;
}

.options ul li:nth-child(3) a:hover {
  background-color: #107c10;
}
</style>
